package cn.danger.com.fragment.user;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.AppCompatButton;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import cn.danger.com.activity.CommonActivity;
import cn.danger.com.activity.R;
import cn.danger.com.fragment.BackHandledFragment;
import cn.danger.com.presenter.ForgetPasswordPresenter;
import cn.danger.com.presenter.ForgetPasswordPresenterImpl;
import cn.danger.com.util.Check;
import cn.danger.com.util.Constants;
import cn.danger.com.view.CommonView;
import cn.danger.com.widget.ClearEditText;

/**
 * Created by 俊 on 2016/12/27.
 */
public class ForgetPasswordFragment extends BackHandledFragment implements View.OnClickListener, CommonView {

    private boolean hadIntercept;
    private View view;
    private CommonActivity activity;
    private Toolbar toolbar;
    private ClearEditText phone_editText;
    private ClearEditText check_code_editText;
    private AppCompatButton send_check_code_button;
    private ClearEditText new_password_editText;
    private AppCompatButton submit_button;
    private ProgressDialog dialog;
    private ForgetPasswordPresenter presenter;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    send_check_code_button.setEnabled(false);
                    send_check_code_button.setText(msg.arg1 + "s后重发");
                    break;
                case 2:
                    send_check_code_button.setEnabled(true);
                    send_check_code_button.setText("获取验证码");
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_forget_password, container, false);

        bindView();
        bindEvent();

        return view;
    }

    @Override
    public void bindView() {
        activity = (CommonActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        phone_editText = (ClearEditText) view.findViewById(R.id.phone_editText);
        check_code_editText = (ClearEditText) view.findViewById(R.id.check_code_editText);
        send_check_code_button = (AppCompatButton) view.findViewById(R.id.send_check_code_button);
        new_password_editText = (ClearEditText) view.findViewById(R.id.new_password_editText);
        submit_button = (AppCompatButton) view.findViewById(R.id.submit_button);

        dialog = new ProgressDialog(activity, ProgressDialog.THEME_HOLO_LIGHT);
        dialog.setMessage("正在执行...");
        dialog.setCanceledOnTouchOutside(false);

        presenter = new ForgetPasswordPresenterImpl(activity, this, handler);
    }

    @Override
    public void bindEvent() {
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
        phone_editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (Check.isMobileNO(String.valueOf(s))) {
                    send_check_code_button.setEnabled(true);
                } else {
                    send_check_code_button.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        send_check_code_button.setOnClickListener(this);
        submit_button.setOnClickListener(this);
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.dismiss();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
        if (message.equals(Constants.SUCCESS_TO_RESET_PASSWORD)) {
            getFragmentManager().popBackStack();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.send_check_code_button:
                presenter.sendCheckCode(phone_editText.getText().toString());
                break;
            case R.id.submit_button:
                presenter.reset(phone_editText.getText().toString(), check_code_editText.getText().toString(),
                        new_password_editText.getText().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }
}